package com.wzxy.pf.loginlog.controller;


import com.wzxy.pf.loginlog.entity.User;
import com.wzxy.pf.loginlog.service.LoginlogService;
import com.wzxy.pf.loginlog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhl
 * @since 2023-04-01
 */
@RestController
@RequestMapping("/loginlog")
public class LoginlogController {
    @Autowired
    private UserService userService;
    @Autowired
    private LoginlogService loginlogService;


    @PostMapping("login")
    public User login(@RequestBody User user,HttpServletRequest request){
       User user1= userService.find(user);
loginlogService.savelogin(user1,request);
        return  user1;
    }

    @Transactional
    @PostMapping("/save")
    public int save(@RequestBody User user,HttpServletRequest request){
        System.out.println(user);
        int a=userService.saveUser(user);
        loginlogService.savelogin(user,request);
        return a;
    }


    @GetMapping("/logout/{id}")
    public User logout(@PathVariable("id") Integer id,HttpServletRequest request){
        User user=userService.findByid(id);
        System.out.println(user);
        loginlogService.savelogout(user,request);
        //销毁
        request.getSession().invalidate();
        return  user;
    }
}

